﻿//活动对象
//var action = { "Actionname": "", "Endtime": "", "GroupList": [{ "Groupname": "", "MinNum": "0", "MaxNum": "0", "VoteResult": [{ "ItemName": "", "Videourl": "" }] }] };


//返回投票选项json对象
function getVoteJson() {
    var action = {};

    //活动
    if ($("#Actionname").val() == "") {
        dialog({
            quickClose: true,
            content: "请输入活动名称",
            height: "1em",
            onclose: function () { $("#Actionname").focus(); },
            follow: $("#Actionname")[0]
        }).show();
        return false;
    }
    //开始时间
    if ($("#Begintime").val() == "") {
        dialog({
            quickClose: true,
            content: "请输入开始时间",
            onclose: function () { $("#Endtime").focus(); },
            follow: $("#Begintime")[0]
        }).show();
        return false;
    } else {
        var d1 = new Date();
        var d2 = new Date($("#Begintime").val().replace(/-/g, "/"));
        d2.setHours(23);
        d2.setMinutes(59);
        d2.setSeconds(59);
        if (d1 > d2) {
            dialog({
                quickClose: true,
                content: "开始时间应晚于当前时间",
                onclose: function () { $("#Endtime").focus(); },
                follow: $("#Begintime")[0]
            }).show();
            return false;
        }
    }
    //结束时间
    if ($("#Endtime").val() == "") {
        dialog({
            quickClose: true,
            content: "请输入结束时间",
            onclose: function () { $("#Endtime").focus(); },
            follow: $("#Endtime")[0]
        }).show();
        return false;
    } else {
        //结束时间和当前时间比较
        var d1 = new Date();
        var d2 = new Date($("#Endtime").val().replace(/-/g, "/"));
        d2.setHours(23);
        d2.setMinutes(59);
        d2.setSeconds(59);
        if (d1 > d2) {
            dialog({
                quickClose: true,
                content: "结束时间应晚于当前时间",
                onclose: function () { $("#Endtime").focus(); },
                follow: $("#Endtime")[0]
            }).show();
            return false;
        }
        //结束时间和开始事件比较
        var begintime = Date.parse($("#Begintime").val());
        if (begintime >= d2) {
            dialog({
                quickClose: true,
                content: "结束时间应晚于开始时间",
                onclose: function () { $("#Endtime").focus(); },
                follow: $("#Endtime")[0]
            }).show();
            return false;
        }
    }

    action["Actionname"] = $("#Actionname").val();
    action["ActionDesc"] = $("#ActionDesc").val();
    action["Begintime"] = $("#Begintime").val();
    action["Endtime"] = $("#Endtime").val();
    action["GroupList"] = [];
    //分组
    var group = $("ul.group>li");
    for (var i = 0; i < group.length; i++) {
        //分组名称
        if ($(group[i]).find(".Groupname").val() == "") {
            dialog({
                quickClose: true,
                content: "请输入分组名称",
                onclose: function () { $(group[i]).find(".Groupname").focus(); },
                follow: $(group[i]).find(".Groupname")[0]
            }).show();
            return false;
        }

        var tips = "";
        //最小必选
        if ($(group[i]).find(".MinNum").val() == "" || isNaN($(group[i]).find(".MinNum").val())) {
            dialog({
                quickClose: true,
                content:  "请输入最小必选数",
                onclose: function () { $(group[i]).find(".MinNum").focus(); },
                follow: $(group[i]).find(".MinNum")[0]
            }).show();
            return false;
        }

        //最大必选
        if ($(group[i]).find(".MaxNum").val() == "" || isNaN($(group[i]).find(".MaxNum").val())) {
            dialog({
                quickClose: true,
                content: "请正确输入最大必选数",
                onclose: function () { $(group[i]).find(".MaxNum").focus(); },
                follow: $(group[i]).find(".MaxNum")[0]
            }).show();
            return false;
        }

        //选项
        var item = $(group[i]).find("ul.item>li");
        if ($(group[i]).find(".MinNum").val() > item.length) {
            //dialog = initDailog($(group[i]).find(".MinNum"), "最小必选数大于总数");
            dialog({
                quickClose: true,
                content: "最小必选数大于总数",
                onclose: function () { $(group[i]).find(".MinNum").focus(); },
                follow: $(group[i]).find(".MinNum")[0]
            }).show();
            return false;
        }
        if ($(group[i]).find(".MaxNum").val() > item.length) {
            dialog({
                quickClose: true,
                content: "最大必选数大于总数",
                onclose: function () { $(group[i]).find(".MaxNum").focus(); },
                follow: $(group[i]).find(".MaxNum")[0]
            }).show();
            return false;
        }
        if ($(group[i]).find(".MinNum").val() > $(group[i]).find(".MaxNum").val()) {
            //dialog = initDailog($(group[i]).find(".MaxNum"), "最小必选数大于最大必选数");
            dialog({
                quickClose: true,
                content: "最小必选数大于最大必选数",
                onclose: function () { $(group[i]).find(".MinNum").focus(); },
                follow: $(group[i]).find(".MinNum")[0]
            }).show();
            return false;
        }
        action.GroupList.push({
            "Groupname": $(group[i]).find(".Groupname").val(),
            "GroupDesc": $(group[i]).find("input[name=groupdesc]").val(),
            "MinNum": $(group[i]).find(".MinNum").val(),
            "MaxNum": $(group[i]).find(".MaxNum").val(),
            "VoteType": $(group[i]).find("input[type=radio][name^=votetype]:checked").val(),
            "VoteResult": []
        });
        for (var j = 0; j < item.length; j++) {
            if ($(item[j]).find(".Itemname").val() == "") {
                dialog({
                    quickClose: true,
                    content: "请输入选项名称",
                    onclose: function () { $(item[j]).find(".Itemname").focus(); },
                    follow: $(item[j]).find(".Itemname")[0]
                }).show();
                return false;
            }
            if ($(item[j]).find(".Videourl").val() != "") {
                //    dialog({
                //        quickClose: true,
                //        content: "请输入视频URL",
                //        onclose: function () { $(item[j]).find(".Videourl").focus(); },
                //        follow: $(item[j]).find(".Videourl")[0]
                //    }).show();
                //    return false;
                //}
                //else {
                var reUrl = new RegExp(/^http:\/\/v.youku.com\/v_show\/id_/gi);
                var url = $(item[j]).find(".Videourl").val();
                if (!reUrl.test(url)) {
                    dialog({
                        quickClose: true,
                        content: "视频URL格式不正确",
                        onclose: function () { $(item[j]).find(".Videourl").focus(); },
                        follow: $(item[j]).find(".Videourl")[0]
                    }).show();
                    return false;
                }
            }
            //var reUrl = new RegExp(/^http:\/\/v.youku.com\/v_show\/id_$.html/gi);
            //var videoId = reUrl.exec($(item[j]).find(".Videourl").val());
            var indexBegin = $(item[j]).find(".Videourl").val().indexOf("id_") + 3;
            var indexEnd = $(item[j]).find(".Videourl").val().indexOf(".html");
            var videoId = $(item[j]).find(".Videourl").val().substring(indexBegin, indexEnd);
            action.GroupList[i].VoteResult.push({
                "ItemName": $(item[j]).find(".Itemname").val(),
                "ImageUrl": $(item[j]).find("input:hidden[name=uploadimgname]").val(),
                "Videourl": videoId,
                "Desc": $(item[j]).find("textarea[name=votedesc]").val()
            });
        }
    }


    return action;

}
